﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace AutoUpdater
{
    public class Resource
    {
        private static ResourceManager Manager;
        private static string Dll = "AutoUpdater.resources.dll";
        private static System.Globalization.CultureInfo currentCulture;

        public static System.Globalization.CultureInfo CurrentCulture {
            get { return currentCulture; }
        }

        static Resource() {

            var appSettings = ConfigurationManager.AppSettings;

            if (appSettings.AllKeys.Contains("Language")) {
                var value = appSettings["Language"];
                currentCulture = new System.Globalization.CultureInfo(value, true);
                Thread.CurrentThread.CurrentUICulture = currentCulture;
                Thread.CurrentThread.CurrentCulture = currentCulture;
            } else {
                currentCulture = System.Globalization.CultureInfo.CurrentCulture;
            }

            var path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), CurrentCulture.Name);
            var assembly = Assembly.LoadFrom(Path.Combine(path, Dll));
            var baseName = string.Format("AutoUpdater.AutoUpdater.{0}", CurrentCulture.Name);
            Manager = new ResourceManager(baseName, assembly);

        }

        public static string GetString(string key) {
            return Manager.GetString(key);
        }
    }
}
